module("framework.core.taskdispatcher.TaskItem", package.seeall)

---@class TaskItem : MiddleClass
local TaskItem = class("TaskItem")

TaskItem.frameCount = 0
TaskItem._itemPool = nil
local TRACKBACK = __G__TRACKBACK__
local _xpcall = xpcall

---创建任务单位池
---@return LuaObjPool
function TaskItem.createPool()
    TaskItem._itemPool = LuaObjPool.New(32, TaskItem._poolNew, TaskItem._poolRelease, TaskItem._poolReset)
    return TaskItem._itemPool
end

function TaskItem._poolNew()
    return TaskItem.New()
end

function TaskItem._poolRelease(luaObj)
    luaObj:release()
end

function TaskItem._poolReset(luaObj)
    luaObj:reset()
end

function TaskItem:reset()
    self.interval = 0
    self.addFrame = 0
    self.timeCount = 0
    self:setCb(nil, nil)
    self.repeatCount = 0
    self.isLoop = false
    self.hasInvoked = false
    self.status = TaskDispatcher.Idle
end

function TaskItem:ctor()
    self:reset()
end

function TaskItem:release()
    self:reset()
end

---setCb
---@param callback fun(obj:any)
---@param cbObj any
function TaskItem:setCb(callback, cbObj)
    self.callback = callback
    self.cbObj = cbObj
end

---update
---@param deltaTime number
function TaskItem:update(deltaTime)
    --如果被标记删除，则直接跳过执行
    if self.status == TaskDispatcher.ToDelete then
        self.repeatCount = 0
        return false
    end
    self.hasInvoked = false

    if self.addFrame >= TaskItem.frameCount then
        return self.hasInvoked
    end

    self.timeCount = self.timeCount + deltaTime
    if self.timeCount < self.interval then
        return self.hasInvoked
    end

    self.hasInvoked = true
    self.timeCount = self.timeCount - self.interval

    --1. IOS lua报错容易闪退 加try catch
    --2. 一个定时器报错，会终止所有定时器的执行，加上try catch处理
    if self.cbObj then
        _xpcall(self.callback, TRACKBACK, self.cbObj)
    else
        _xpcall(self.callback, TRACKBACK)
    end

    self.repeatCount = self.repeatCount - 1
    return self.hasInvoked
end

function TaskItem:logStr()
    return "callback = "..tostring(self.callback).." cbObj = "..tostring(self.cbObj).." interval = "..self.interval..
        " addFrame = "..self.addFrame.." timeCount = "..self.timeCount.." repeatCount = "..self.repeatCount.." isLoop = "..
        tostring(self.isLoop).." hasInvoked = "..tostring(self.hasInvoked).." status = "..self.status
end

return TaskItem
